package com.chenye.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Auther:chenye
 * @Data:2018/8/22
 * @Description: 让子线程获取到父线程设置的值
 */
public class TestInheritableThreadLocal {
    private static InheritableThreadLocal threadLocal = new InheritableThreadLocal();

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        threadLocal.set(123);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.err.println("childThread:"+threadLocal.get());
            }
        });
        thread.start();
        System.err.println("main:"+threadLocal.get());

        Callable callable = new Callable(){
            @Override
            public Object call() throws Exception {
                return "123";
            }
        };
        Future future = Executors.newScheduledThreadPool(1).submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return "123";
            }
        });
        System.out.println(future.get());
    }
}
